﻿using SL5.Common;

namespace System.Windows.Markup
{
	/// <summary>
	/// Type extension.
	/// </summary>
	public sealed class TypeExtension : MarkupExtension
	{
		#region Public Properties 
		/// <summary>
		/// Gets or sets the name of the type.
		/// </summary>
		/// <value>The name of the type.</value>
		public string TypeName
		{
			get;
			set;
		}
		#endregion Public Properties 

		#region Public Methods 
		/// <summary>
		/// When implemented in a derived class, returns an object that is provided as the value of the target property for this markup extension.
		/// </summary>
		/// <param name="serviceProvider">A service provider helper that can provide services for the markup extension.</param>
		/// <returns>The object value to set on the property where the extension is applied.</returns>
		public override object ProvideValue(IServiceProvider serviceProvider)
		{
			if (String.IsNullOrEmpty(this.TypeName))
			{
				throw new InvalidOperationException("TypeName is not set.");
			}
			return serviceProvider.GetService<IXamlTypeResolver>().Resolve(this.TypeName);
		}
		#endregion Public Methods 
	}
}